﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Xunit;

namespace FastReflection.Tests
{
    public class PropertyTest
    {
        private class Employee
        {
            public Employee()
            {
                this.Name = "Mike";
                this._ID = Guid.NewGuid();
                this.Age = null;
            }

            private Guid _ID;

            public string Name { get; set; }

            public Guid ID
            {
                get
                {
                    return _ID;
                }
            }

            private int? _Age;

            public int? Age
            {
                set
                {
                    _Age = value;
                }
            }
        }

        [Fact]
        public void Verify_Get()
        {
            var nameProperty = typeof(Employee).GetProperty("Name").ToFastProperty();
            var idProperty = typeof(Employee).GetProperty("ID").ToFastProperty();

            Assert.True(nameProperty.CanWrite);
            Assert.False(idProperty.CanWrite);
        }

        [Fact]
        public void Verify_Set()
        {
            var nameProperty = typeof(Employee).GetProperty("Name").ToFastProperty();
            var ageProperty = typeof(Employee).GetProperty("Age").ToFastProperty();

            Assert.True(nameProperty.CanRead);
            Assert.False(ageProperty.CanRead);
        }

        [Fact]
        public void Run_Set_Basic()
        {
            Employee e = new Employee();
            var nameProperty = typeof(Employee).GetProperty("Name").ToFastProperty();
            var ageProperty = typeof(Employee).GetProperty("Age").ToFastProperty();

            Assert.True((nameProperty.Get(e) as string) == "Mike");
            nameProperty.Set(e, "Jake");
            Assert.True((nameProperty.Get(e) as string) == "Jake");

            Assert.DoesNotThrow(() =>
            {
                ageProperty.Set(e, 4);
            });
        }
    }
}
